package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.ShopBrand;
import com.hyyt.shopjsp.product.pojo.ShopBrandType;
import com.hyyt.shopjsp.product.service.IShopBrandService;
import com.hyyt.shopjsp.product.service.IShopBrandTypeService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/7/28.
 */

@Controller
@RequestMapping("/back/brandType")
public class ShopBrandTypeController extends BaseController{

    private Logger logger = Logger.getLogger(ShopBrandTypeController.class);
    private String basePath = "/back/shop/product/shopProductType/brandType/";

    @Resource
    private IShopBrandService shopBrandService;
    @Resource
    private IShopBrandTypeService shopBrandTypeService;

    /**
     * 根据商品分类ID查询已添加的商品品牌
     * @param productTypeId
     * @param page
     * @param response
     */
    @RequestMapping(value = "listSelectedBrandByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void listSelectedBrandByProductTypeId(Integer productTypeId, String brandName, String isShow, Page page,HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(brandName.trim())){
            params.put("brandName",brandName);
        }
        if(Utils.objectIsNotEmpty(isShow)){
            params.put("isShow",isShow);
        }
        if(Utils.objectIsNotEmpty(productTypeId)){
            params.put("productTypeId",productTypeId);
        }
        int totalRecordCount = shopBrandService.countSelectedBrandByProductTypeId(params);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<Map<String,Object>> shopBrandList = shopBrandService.querySelectedBrandByProductTypeId(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopBrandList);
        ResultMsg.create(params,response);
    }

    /**
     * 根据商品分类ID查询未添加的商品品牌
     * @param productTypeId
     * @param page
     * @param brandName
     * @param isShow
     * @param isRecommend
     * @param isHomePage
     * @param response
     */
    @RequestMapping(value = "listUnselectedBrandByProductTypeId",method = RequestMethod.POST)
    @ResponseBody
    public void listUnselectedBrandByProductTypeId(String brandName,String isShow,String isRecommend,String isHomePage,Integer productTypeId,Page page, HttpServletResponse response){
        Map<String,Object> params = new HashMap<>();
        if(Utils.objectIsNotEmpty(productTypeId)){
            params.put("productTypeId",productTypeId);
        }
        if(Utils.objectIsNotEmpty(brandName)){
            params.put("brandName",brandName.trim());
        }if(Utils.objectIsNotEmpty(isShow)){
            params.put("isShow",isShow);
        }if(Utils.objectIsNotEmpty(isRecommend)){
            params.put("isRecommend",isRecommend);
        }if(Utils.objectIsNotEmpty(isHomePage)){
            params.put("isHomePage",isHomePage);
        }
        int totalRecordCount = shopBrandService.countUnselectedBrandByProductTypeId(params);
        page.setPageInfo(page.getPageSize(),totalRecordCount,page.getCurrentPage());
        List<ShopBrand> shopBrandList = shopBrandService.queryUnselectedBrandByProductTypeId(params,page.getPageRecordBeginIndex(),page.getPageSize());
        params.clear();
        params.put("total",totalRecordCount);
        params.put("rows",shopBrandList);
        ResultMsg.create(params,response);
    }

    /**
     * 添加新的品牌给商品分类
     * @param brandIds
     * @param isShows
     * @param productTypeId
     * @return
     */
    @RequestMapping(value = "saveSelectedBrandType",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveSelectedBrandType(Integer productTypeId, String brandIds,String isShows) {
        if (Utils.objectIsNotEmpty(productTypeId) && Utils.objectIsNotEmpty(brandIds) && Utils.objectIsNotEmpty(isShows)) {
            shopBrandTypeService.saveShopBrandType(productTypeId,brandIds,isShows);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false, "保存失败，请重新操作");
        }
    }

    /**
     * 删除商品分类下的品牌
     * @param productTypeId 商品分类id
     * @param brandId 品牌id
     * @return
     */
    @RequestMapping(value = "deleteBrandType",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteBrandType(Integer productTypeId,Integer brandId){
        if(Utils.objectIsNotEmpty(productTypeId) && Utils.objectIsNotEmpty(brandId)){
            shopBrandTypeService.deleteBrandTypeByProductTypeIdAndBrandId(productTypeId,brandId);
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false, "删除失败，请重新操作");
        }
    }

    /**
     * 更改显示状态
     * @param brandId
     * @param productTypeId
     * @return
     */
    @RequestMapping(value = "updateIsShowState",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg updateIsShowState(Integer brandId,Integer productTypeId){
        if( Utils.objectIsNotEmpty(brandId) && Utils.objectIsNotEmpty(productTypeId)){
            Map<String,Object> params = new HashMap<>();
            params.put("brandId",brandId);
            params.put("productTypeId",productTypeId);
            List<ShopBrandType> shopBrandTypeList = shopBrandTypeService.queryShopBrandType(params);
            for(ShopBrandType shopBrandType:shopBrandTypeList){
                int isShow = shopBrandType.getIsShow();
                if(isShow==1){
                    shopBrandType.setIsShow(0);
                }else{
                    shopBrandType.setIsShow(1);
                }
                shopBrandTypeService.updateShopBrandType(shopBrandType);
            }
            return  ResultMsg.create(true);
        }else {
            return  ResultMsg.create(false);
        }
    }

    /**
     * 复制上级分类
     * @param productTypeId
     * @return
     */
    @RequestMapping(value = "copyParentProductType",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg copyParentProductType(Integer productTypeId){
        if(Utils.objectIsNotEmpty(productTypeId)){
            Map<String,Object> params = new HashMap<>();
            params.put("productTypeId",productTypeId);
            List<ShopBrandType> shopBrandTypeList = shopBrandTypeService.queryShopBrandType(params);
            for(ShopBrandType shopBrandType:shopBrandTypeList){
                shopBrandType.setProductTypeId(productTypeId);
                shopBrandType.setIsShow(0);
                shopBrandTypeService.saveShopBrandType(shopBrandType);
            }
            return  ResultMsg.create(true);
        }else {
            return  ResultMsg.create(false);
        }
    }

}
